using Data;
using Framework.Audio;
using Framework.FSMLite;
using Framework.Timer;
using Framework.UI;
using UnityEngine;
using View;

namespace GameSystem
{
    public class StateGame : StateMachine<GameEnum>
    {

        internal override void OnEnter(params object[] args)
        {
            AudioManager.Instance.PlayBGM("BGM_2", 1, 1f);
            AudioManager.Instance.PlayBSE("BSE_wind2", 0.75f, 2f , true);
            if (GameManager.Instance.GameRoom.NowLevel >= 0)
            {
                AudioManager.Instance.PlayBSE("Game_BSE2", 1, 0.75f , true);
            }
            GameManager.Instance.GameRoom.CreateScene();
            GameUpdateMgr.Instance.AddUpdater(this.DoUpdate);
        }



        internal override void OnExit()
        {
            GameManager.Instance.ChangeVCam(5 , GameManager.Instance.GameRoom.GameRoomData.Level);
            UIManager.Instance.CloseAllView();
            GameUpdateMgr.Instance.RemoveUpdater(this.DoUpdate);
        }
        
        private void DoUpdate()
        {
            if (GameManager.Instance.IsGaming)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Ray        ray = GameManager.Instance.MainCamera.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit , 100 , 1 << 9))
                    {
                        if (hit.collider.CompareTag("Hud"))
                        {
                            UIManager.Instance.EventManager.SendEvent(EventCode.SelectCube , hit.collider.GetComponent<CubeHud>().Cube);
                        }
                        else
                        {
                            UIManager.Instance.EventManager.SendEvent(EventCode.SelectCube , hit.collider.GetComponent<GameLightCube>());
                        }
                    }
                    else
                    {
                        UIManager.Instance.EventManager.SendEvent(EventCode.UnSelectCube);
                    }
                }
            }
        }

    }
}